<?php
/**
 * Request wrapper
 * @static
 * @author Kirill Egorov 2011
 */
class Request
{	
	protected static $instance = null;
	protected static $_delimiter = '/';
	protected static $_extension = '';
	protected static $_updatedPost = array();
	
	protected $_path = array();
	protected $_uri;

	protected function __clone()
	{
	}

	/**
	 * @return Request
	 */
	static public function getInstance()
	{
		if(is_null(self::$instance))
			self::$instance = new self();
		
		return self::$instance;
	}
	/**
	 * Set params separator
	 * Enter description here ...
	 * @param unknown_type $delimeter
	 */
	static public function setDelimetr($delimeter)
	{
		self::$_delimiter = $delimeter;
	}
	/**
	 * Set pagename extension (postfix like ".html" , ".xhtml" whatever...)
	 * @param string $extension
	 */
	static public function setExtension($extension)
	{
		self::$_extension = $extension;
	}

	protected function __construct()
	{
		$string = strtolower($_SERVER['REQUEST_URI']);
		
		if(strpos($string , '?'))
			$string = substr($string , 0 , strpos($string , '?'));
		
		$string = str_ireplace(array(			
				'.html' , 
				'.html' , 
				'.php' , 
				'.xml' , 
				'.phtml' , 
				'.json'
		) , '' , $string);
		
		$this->_uri = preg_replace("/[^A-Za-z0-9_\.\-\/]/i" , '' , $string);
		$this->getPaths();
	}

	public function getPaths()
	{
		if(self::$_delimiter === '/')
		{
			$array = explode(self::$_delimiter , $this->_uri);
			for($i = 1, $sz = sizeof($array); $i < $sz; $i ++)
				$this->_path[] = $array[$i];
		}
		else
		{
			$str = substr($this->_uri , 1);
			$array = explode(self::$_delimiter , $str);
			for($i = 0, $sz = sizeof($array); $i < $sz; $i ++)
				$this->_path[] = $array[$i];
		}
	}

	/**
	 * Get request path
	 * @param integer $index
	 * @return mixed
	 */
	public function getPath($index)
	{
		if(isset($this->_path[$index]) && ! empty($this->_path[$index]))
			return $this->_path[$index];
		else
			return false;
	}

	/**
	 * Create url
	 * @param array $paths
	 * @return string
	 */
	static public function url(array $paths)
	{
		return '/' . implode(self::$_delimiter , array_map('strtolower' , $paths)) . self::$_extension;
	}

	/**
	 * Get list of sended files
	 * @return array
	 */
	static public function files()
	{
		if(! isset($_FILES) || empty($_FILES))
			return array();
		
		$result = array();
		
		if(empty($_FILES))
			return $result;
		
		foreach($_FILES as $key => $data)
		{
			if(! isset($data['name']))
				continue;
			
			if(! is_array($data['name']))
			{
				$result[$key] = $data;
			}
			else
			{
				foreach($data['name'] as $subkey => $subval)
				{
					$result[$key][$subkey] = array(					
							'name' => $data['name'][$subkey] , 
							'type' => $data['type'][$subkey] , 
							'tmp_name' => $data['tmp_name'][$subkey] , 
							'error' => $data['error'][$subkey] , 
							'size' => $data['size'][$subkey]
					);
				}
			}
		}
		return $result;
	}

	/**
	 * Get param from $_GET
	 * @param string $name
	 * @param string $type - value type
	 * @param mixed $default - default value
	 * @return mixed
	 */
	static public function get($name , $type , $default)
	{
		if(! isset($_GET[$name]))
			return $default;
		else
			return Filter::filterValue($type , $_GET[$name]);
	}

	/**
	 * Get param from $_POST
	 * @param string $name
	 * @param string $type - value type
	 * @param mixed $default - default value
	 * @return mixed
	 */
	static public function post($name , $type , $default)
	{
		
		if(array_key_exists($name , self::$_updatedPost))
			return self::$_updatedPost;
		
		if(! isset($_POST[$name]))
			return $default;
		else
			return Filter::filterValue($type , $_POST[$name]);
	}

	/**
	 * Redefine posted value
	 * @param string $name
	 * @param mixed $value
	 */
	static public function updatePost($name , $value)
	{
		self::$_updatedPost[$name] = $value;
	}

	/**
	 * Get all variables from post request
	 * @return array
	 */
	static public function postArray()
	{
	    return array_merge($_POST , self::$_updatedPost);		
	}
}